package com.cssw.fyzb.busi.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 身份证处理
 *
 * @Author zhouxu
 * @create 2020-08-25 13:58
 */
public class IDCardUtil {

    /**
     * 根据身份证号获取年龄
     *
     * @param certId
     * @return
     */
    public static int getAgeByCertId(String certId) throws Exception {
        String birthday = "";
        if (certId.length() == 18) {
            birthday = certId.substring(6, 10) + "/"
                    + certId.substring(10, 12) + "/"
                    + certId.substring(12, 14);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date now = new Date();
        Date birth = sdf.parse(birthday);
        long intervalMilli = now.getTime() - birth.getTime();
        int age = (int) (intervalMilli / (24 * 60 * 60 * 1000)) / 365;

        return age;
    }

    public static void main(String args[]) throws Exception {

        String idCard = "430426199705017678";

        System.out.println(getAgeByCertId(idCard));

    }

}
